export function formatDate(dateStr) {
    // Ensure input is UTC time
    const date = new Date(dateStr);
    const now = new Date();
    
    // Get local time zone offset (minutes)
    const timeZoneOffset = now.getTimezoneOffset();
    
    // Calculate time difference (considering time zone)
    const diff = now - (date.getTime() - timeZoneOffset * 60000);
    
    // Convert to seconds
    const seconds = Math.floor(diff / 1000);

    // if seconds less than 10, return 'just now'
    if (seconds < 10) {
        return 'just now';
    }
    
    // 60 seconds less than 1 minute
    if (seconds < 60) {
        return `${seconds} seconds ago`;
    }
    
    // convert to minutes
    const minutes = Math.floor(seconds / 60);
    
    // 60 minutes less than 1 hour
    if (minutes < 60) {
        return `${minutes} minutes ago`;
    }
    
    // convert to hours
    const hours = Math.floor(minutes / 60);
    
    // 24 hours less than 1 day
    if (hours < 24) {
        return `${hours} hours ago`;
    }
    
    // use toLocaleString and specify timezone to UTC
    return date.toLocaleString('zh-CN', {
        timeZone: 'UTC',
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
    });
}